﻿#region Header
//************************************************************************************
// Name: Mailer
// Description: Mailer class to handle EMail's
// Created On:  02-Aug-2011
// Created By:  Swathi
// Last Modified On:
// Last Modified By:
// Last Modified Reason:
//*************************************************************************************
#endregion Header
using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Web;

namespace Base.Utils
{
    public sealed class Mailer
    {
        #region Events

        /// <summary>
        /// Occurs after an e-mail has been sent. The sender is the MailMessage object.
        /// </summary>
        public static event EventHandler<EventArgs> EmailFailed;

        /// <summary>
        /// Occurs after an e-mail has been sent. The sender is the MailMessage object.
        /// </summary>
        public static event EventHandler<EventArgs> EmailSent;

        #endregion Events

        #region Methods

        public static bool IsInWebContext()
        {
            return HttpContext.Current != null;
        }

        /// <summary>
        /// Sends a MailMessage object using the SMTP settings.
        /// </summary>
        public static void SendMailMessage(MailMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message cannot be null");
            }

            try
            {
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;

                var smtp = new SmtpClient(IsInWebContext() ? AppSettings.Instance.smtp_server_name : "localhost");
                if (IsInWebContext())
                {
                    smtp.Credentials = new NetworkCredential(AppSettings.Instance.smtp_user_name,
                                                             AppSettings.Instance.smtp_password);
                    smtp.Port = AppSettings.Instance.smtp_port;
                }
                else
                {
                    smtp.Credentials = new NetworkCredential("noreply@reminders.supertrainerhq.com", "Power@1234");
                    smtp.Port = 25;
                }
                smtp.EnableSsl = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(message);
                OnEmailSent(message);
            }
            catch (SmtpException)
            {
                OnEmailFailed(message);
                //throw;
            }
            finally
            {
                // Remove the pointer to the message object so the GC can close the thread.
                message.Dispose();
            }
        }

        /// <summary>
        /// Sends the mail message asynchronously in another thread.
        /// </summary>
        /// <param name="message">The message to send.</param>
        public static void SendMailMessageAsync(MailMessage message)
        {
            ThreadPool.QueueUserWorkItem(delegate { SendMailMessage(message); });
        }

        private static void OnEmailFailed(MailMessage message)
        {
            if (EmailFailed != null)
            {
                EmailFailed(message, new EventArgs());
            }
        }

        private static void OnEmailSent(MailMessage message)
        {
            if (EmailSent != null)
            {
                EmailSent(message, new EventArgs());
            }
        }

        #endregion Methods
    }
}